package cn.study.springsecurityoauth2.common;



import cn.study.springsecurityoauth2.enums.ErrorEnum;

import java.io.Serializable;

/**
 * @ClassName BaseResponse
 * @Author swj
 * @Date 2020/10/10 10:08
 */
public class BaseResponse<T> implements Serializable {

    private static final long serialVersionUID = 9211889136173018364L;

    /**
     * 成功返回码
     */
    private static final int SUCCESS_CODE = 200;

    /**
     * 成功返回信息
     */
    private static final String SUCCESS_MSG = "SUCCESS";

    /**
     * 返回信息标识码
     */
    private int code = SUCCESS_CODE;

    /**
     * 返回信息描述信息
     */
    private String msg = SUCCESS_MSG;

    /**
     * 响应内容-默认为空
     */
    private T data = null;


    /**
     * 返回空的正确返回
     * @return
     */
    public static BaseResponse retOk(){
        return new BaseResponse();
    }

    /**
     * 有内容的成功信息返回
     * @param data
     * @param <T>
     * @return
     */
    public static <T> BaseResponse<T> retOk(T data){
        BaseResponse<T> baseResponse = new BaseResponse<T>();
        baseResponse.setData(data);
        return baseResponse;
    }

    /**
     * 失败版本返回 enum 版本
     * @param errorEnum
     * @return
     */
    public static BaseResponse retFail(ErrorEnum errorEnum){
        return retFail(errorEnum.getRespCode(),errorEnum.getRespMsg());
    }

    /**
     * 无失败内容返回
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> BaseResponse<T> retFail(int code,String msg){
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(code);
        response.setMsg(msg);
        return response;
    }

    /**
     * 有失败的返回格式 enum版本
     * @param errorEnum
     * @param data
     * @param <T>
     * @return
     */
    public static <T> BaseResponse<T> retFail(ErrorEnum errorEnum,T data) {
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(errorEnum.getRespCode());
        response.setMsg(errorEnum.getRespMsg());
        response.setData(data);
        return response;
    }

    /**
     * 有失败的返回格式
     * @param code
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static <T> BaseResponse<T> retFail(int code, String msg, T data){
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(code);
        response.setMsg(msg);
        response.setData(data);
        return response;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }
}
